using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Typing_GUI.CLASS;
using System.Collections;

namespace Typing_GUI
{
    public partial class frmCreateExam : Form
    {
        public frmCreateExam()
        {
            InitializeComponent();
        }
        public int soBai=1;
        public ArrayList mangBaiTapCuaExam = new ArrayList();
        public void LoadBaiTapCuaExam()
        {
            dgvDanhSachBaiTapCuaExam.Rows.Clear();
            for (int i = 0; i < mangBaiTapCuaExam.Count; i++)
            {
                CBaiTap bt = (CBaiTap)mangBaiTapCuaExam[i];
                DataGridViewRowCollection r = this.dgvDanhSachBaiTapCuaExam.Rows;
                r.Add((i + 1).ToString(), bt.Mabaitap, bt.Tenbaitap);
            }
            dgvDanhSachBaiTapCuaExam.SelectionMode = DataGridViewSelectionMode.CellSelect;
            dgvDanhSachBaiTapCuaExam.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }
        public bool KiemTraBaiTapTrongExam(CBaiTap bt)
        {
            for (int i = 0; i < mangBaiTapCuaExam.Count; i++)
            {
                CBaiTap temp = (CBaiTap)mangBaiTapCuaExam[i];
                if (temp.Mabaitap == bt.Mabaitap)
                    return false;
            }
            return true;
        }
        public bool KiemTraDuLieuDauVao()
        {
            
            if (txtTenExam.Text == "")
            {
                MessageBox.Show("Exam Name is  blank !! ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                txtTenExam.Focus();
                return false;
            }
            else if (txtMoTa.Text == "")
            {
                MessageBox.Show("you must write some in Decription  !! ", "Warning  !!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                txtMoTa.Focus();
                return false;
            }

            //else if (dgvDanhSachBaiTapCuaExam.Rows.Count <1)
            //{
            //    MessageBox.Show("you must add some exercise in List Exam !! ", "Warning  !!", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            //    dgvDanhSachBaiTapCuaExam.Focus();
            //    return false;          
            //}

            return true;
        }
        private void frmCreateExam_Load(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = CBaiTap.LoadDanhSachBaiTap(1);
            
                int stt = 1;
                foreach (DataRow row in dt.Rows)
                {
                    DataGridViewRowCollection r = this.dgvDanhSachBaiTap.Rows;
                    r.Add(stt.ToString(),row["MaBaiTap"],row["TenBaiTap"]);
                    stt++;
                }
                dgvDanhSachBaiTap.SelectionMode = DataGridViewSelectionMode.CellSelect;
                dgvDanhSachBaiTap.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            }
            catch
            {
                MessageBox.Show("Error connecting database ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error); 
            }
        }

        private void btnTao_Click(object sender, EventArgs e)
        {
            if (KiemTraDuLieuDauVao())
            {
                try
                {
                    CToChucLuyenTap toChuc = new CToChucLuyenTap();
                    toChuc.Tenluyentap = txtTenExam.Text;
                    toChuc.Mota = txtMoTa.Text;
                  //  toChuc.createTCLT(); co van de
                    toChuc.ThemTCLT();
                    int matc = CToChucLuyenTap.LayMax();
                    for (int i = 0; i < mangBaiTapCuaExam.Count; i++)
                    {
                        CBaiTap bt = (CBaiTap)mangBaiTapCuaExam[i];
                        CChiTietLuyenTap chiTiet = new CChiTietLuyenTap();
                        chiTiet.Mabaitap = bt.Mabaitap;
                        chiTiet.Maluyentap = matc;
                        chiTiet.createUnit();
                    }
                    MessageBox.Show("Create successful \n Check in ManageExam ", "Success !!", MessageBoxButtons.OK, MessageBoxIcon.None);
                    txtTenExam.Text = "";
                    txtMoTa.Text = "";
                    txtTenExam.Focus();
                    dgvDanhSachBaiTapCuaExam.Rows.Clear();
                    mangBaiTapCuaExam.Clear();
                    
                }
                catch
                {
                    MessageBox.Show("Error connecting database ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);         
                }
            }
           
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBaiTap.SelectedRows.Count > 0 )
            {
                if (KiemTraDuLieuDauVao() == true)
                {
                    for (int i = dgvDanhSachBaiTap.SelectedRows.Count - 1; i >= 0;i-- )
                    {
                        CBaiTap bt = new CBaiTap();
                        bt.Mabaitap = (int)dgvDanhSachBaiTap.SelectedRows[i].Cells[1].Value;
                        bt.Tenbaitap = dgvDanhSachBaiTap.SelectedRows[i].Cells[2].Value.ToString();
                        if (KiemTraBaiTapTrongExam(bt) == true)
                            mangBaiTapCuaExam.Add(bt);
                        else
                        {
                            MessageBox.Show("This Exercise is exists !! :  "+bt.Tenbaitap, "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                        }
                    }
                }
            }
            else
            {
                MessageBox.Show("Not choose exercise to add in a exam ", "ERROR !!", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            LoadBaiTapCuaExam();
        }

        private void btnXoaBaiTap_Click(object sender, EventArgs e)
        {
            if (dgvDanhSachBaiTapCuaExam.SelectedRows.Count > 0)
            {
                for (int i = dgvDanhSachBaiTapCuaExam.SelectedRows.Count - 1;i>=0 ; i--)
                {
                    int index = dgvDanhSachBaiTapCuaExam.SelectedRows[i].Index;
                    DialogResult dl = MessageBox.Show("Are you sure you want to delete it : "+dgvDanhSachBaiTapCuaExam.SelectedRows[i].Cells[2].Value.ToString(),"Warning !!!",MessageBoxButtons.OKCancel,MessageBoxIcon.Warning);
                    if(dl==DialogResult.OK)
                      mangBaiTapCuaExam.RemoveAt(index);
                }
                LoadBaiTapCuaExam();
            }
            else
                MessageBox.Show("Not choose exercise to delete  ", "Warning!!", MessageBoxButtons.OK, MessageBoxIcon.Warning); 
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            foreach (Form frm in Application.OpenForms)
            {
                if (frm is frmManageExam)
                {
                    ((frmManageExam)frm).LoadDuLieuCuaExamList();
                    break;
                }
            }
            this.Close();
        }
    }
}